#include <stdio.h>
#include <string.h>
#include "buildsettings.h"
#include "list.h"
void Help(void);
void TestList(void);

//! Defines a single mapping from a string to a function.
typedef struct LCaseMapping
{
	char *String;
	void (*Function)(void);
} CaseMapping;

//! Defines a set of strings mapped to functions.
CaseMapping Mapping[] =
{
	{"help", Help},
	{"list-operations", TestList}
};

/*!
  Tester entry point.
*/
int main(int argc, char *argv[])
{
	int i=0;
	int numMappings = sizeof(Mapping)/sizeof(struct LCaseMapping);
	void (*func)(void) = 0x0;

	if (argc <= 1)
	{
		printf("Enter a test case as an argument.\n\n");
		return 0;
	}
	while (i < numMappings)
	{
		if (strcmp(argv[1], Mapping[i].String) == 0)
			func = Mapping[i].Function;
		i=i+1;
	}
	if (func != 0x0)
		func();
	return 0;
}

// Test Cases Follow:

/*!
  Print the array of mappings.
  This function should be in every tester
*/
void Help(void)
{
	int i=0;
	int numMappings = sizeof(Mapping)/sizeof(struct LCaseMapping);
	printf("Mappings:\n");
	while (i < numMappings)
	{
		printf("  >%s\n", Mapping[i].String);
		i=i+1;
	}
}

void TestGetByValue(List *list)
{
	char *str;
	void *v;
	str = "b";
	printf("Searching for '%s'...", str);
	v = list->GetByValue(list, str, 1, 1);
	if (v != 0x0) {
		printf("found '%s'!\n", (char*)v);
	}
	else {
		printf("No string found for '%s'\n", str);
	}

	str = "gh";
	printf("Searching for '%s'...", str);
	v = list->GetByValue(list, str, 2, 0);
	if (v != 0x0) {
		printf("found '%s'!\n", (char*)v);
	}
	else {
		printf("No string found for '%s'\n", str);
	}

	str = "ghi";
	printf("Searching for '%s'...", str);
	v = list->GetByValue(list, str, 2, 0);
	if (v != 0x0) {
		printf("found '%s'!\n", (char*)v);
	}
	else {
		printf("No string found for '%s'\n", str);
	}
	
}
void TestList(void)
{
	printf("Creating List\n");
	List list  = new(List);
	printf("Appending Values\n");
	char *str="abc";
	list.Append(&list, str);
	str="def";
	list.Append(&list, str);
	str="ghi";
	list.Append(&list, str);
	str = "ghijklmnopqrstuvwxyz";
	list.Append(&list, str);
	str = "abb";
	list.Append(&list, str);

	ListNode *node = list.Head;
	printf("List Contents:\n");
	while (node != 0x0)
	{
		printf("  %s\n", (char*)node->Value);
		node = node->Next;
	}
	printf("Testing GetByValue:\n");
	TestGetByValue(&list);

	printf("Done!\n");
}
